import { Context } from 'koa'
import { query } from '../config/db'
import { select, success, TSqlAndValues, update } from '../utils'
import { branchController } from '../controllers/branch'
import { devGit } from '../utils/git'
import { pool } from '../config/db'
import { developController } from './develop'

export class UserController {
  static async getUsers(ctx: Context) {
    try {
      const result = await query('SELECT * FROM users')
      ctx.body = { data: result }
    } catch (error) {
      ctx.status = 500
      ctx.body = { error: 'Database query failed' }
    }
  }
  static async getUserInfo(ctx: Context) {
    const { id } = ctx.request.body as any
    const [rows]: [any, any] = ((await select('users', { id })) || [{}]) as [any, any]
    delete rows[0].password
    ctx.body = success(ctx, { ...rows[0], userId: rows[0].id, id: undefined })
  }
  static async createUserBranch(ctx: Context) {
    const { id, ...rest } = ctx.request.body as any
    await devGit[ctx.request.headers.nickname as any].createBranch(ctx)
    const responseData = { develop_branch_id: NaN }
    const [result]: [any, any] = await branchController.addBranch(ctx)
    responseData.develop_branch_id = result.insertId
    const connection = await pool.getConnection()
    try {
      await connection.beginTransaction()
      const { sql, values } = (await update(
        'users',
        { id },
        {
          develop_branch_id: responseData.develop_branch_id,
          develop_system_id: rest.develop_system_id
        },
        true
      )) as TSqlAndValues
      const { sql: sql2, values: values2 } = (ctx.request.body as any).endDevelop
        ? ((await update(
            'branches',
            { id: rest.oldBranchId },
            { coding_developer: '', coding_developer_token: '' },
            true
          )) as TSqlAndValues)
        : ({ sql: '', values: [] } as TSqlAndValues)
      await connection.execute(sql, values)
      sql2 && (await connection.execute(sql2, values2))
      await connection.commit()
      //   ;(ctx.request.body as any).endDevelop && developController.endDevelop(ctx)
    } finally {
      connection.release()
    }
    ctx.body = success(ctx, responseData, '服务器创建分支成功，并成功推送到gitlab远程仓库')
  }
  static async switchUserBranch(ctx: Context) {
    const { msg } = await devGit[ctx.request.headers.nickname as any].switchBranch(ctx)
    const { id, develop_system_id, develop_branch_id, oldBranchId } = ctx.request.body as any
    const connection = await pool.getConnection()
    try {
      await connection.beginTransaction()
      const { sql, values } = (await update(
        'users',
        { id },
        { develop_system_id, develop_branch_id },
        true
      )) as TSqlAndValues
      const { sql: sql2, values: values2 } = (ctx.request.body as any).endDevelop
        ? ((await update(
            'branches',
            { id: oldBranchId },
            { coding_developer: '', coding_developer_token: '' },
            true
          )) as TSqlAndValues)
        : ({ sql: '', values: [] } as TSqlAndValues)
      await connection.execute(sql, values)
      sql2 && (await connection.execute(sql2, values2))
      await connection.commit()
      //   ;(ctx.request.body as any).branch_id = (ctx.request.body as any).develop_system_id
      //   ;(ctx.request.body as any).endDevelop && developController.endDevelop(ctx)
    } finally {
      connection.release()
    }
    ctx.body = success(ctx, {}, msg)
  }
}
